﻿
var net_ID = new Array();

var WBEM = COM.createObject( "WbemScripting.SWbemLocator.1" );
var wmi = WBEM.ConnectServer( ".", "root/cimv2" );

var my_net_in = 0;
var my_net_out = 0;
var my_net_in_last = 0;
var my_net_out_last = 0;
var my_net_in_diff = 0;
var my_net_out_diff = 0;
var my_net_bw = 1;

function GenerateDeviceList() {
	// This is a horrible, horrible way to iterate through a loop...
	// Unfortunately, we don't really get a count of interfaces, so this is the best we can do
	for(var i=1;i<32;i++) {
		try {
			if (wmi.Get('Win32_NetworkAdapterConfiguration.Index=' + i).Properties_.Item("IPEnabled").Value) {
				net_ID[i] = wmi.Get('Win32_NetworkAdapterConfiguration.Index=' + i).Properties_.Item("Description").Value.replace(/\//g, "_");
				my_regex = /\//g;
				net_ID[i] = net_ID[i].replace(my_regex, "_")
				my_regex = /\(/g;
				net_ID[i] = net_ID[i].replace(my_regex, "[");
				my_regex = /\)/g;
				net_ID[i] = net_ID[i].replace(my_regex, "]");
				my_regex = /#/g;
				net_ID[i] = net_ID[i].replace(my_regex, "_");
				my_regex = /\™/g;
				net_ID[i] = net_ID[i].replace(my_regex, "[TM]");
				print("Found(A) " + i + ": " + net_ID[i]);
				if(TestDevice(net_ID[i])) {
					print("--> Test OK");
					net_NAME[i] = wmi.Get('Win32_NetworkAdapter.DeviceID=' + i).Properties_.Item("Description").Value;
					if(net_USED == -1) net_USED = i;
				} else {
					print("--> Test Failed");
				}
			}
		} catch (e) { }
	}
	i = 31;
	try {
		while (++i) {
			if (wmi.Get('Win32_NetworkAdapterConfiguration.Index=' + i).Properties_.Item("IPEnabled").Value) {
				net_ID[i] = wmi.Get('Win32_NetworkAdapterConfiguration.Index=' + i).Properties_.Item("Description").Value.replace(/\//g, "_");
				my_regex = /\//g;
				net_ID[i] = net_ID[i].replace(my_regex, "_")
				my_regex = /\(/g;
				net_ID[i] = net_ID[i].replace(my_regex, "[");
				my_regex = /\)/g;
				net_ID[i] = net_ID[i].replace(my_regex, "]");
				print("Found(B) " + i + ": " + net_ID[i]);
				if(TestDevice(net_ID[i])) {
					print("--> Test OK");
					net_NAME[i] = wmi.Get('Win32_NetworkAdapter.DeviceID=' + i).Properties_.Item("Description").Value;
					if(net_USED == -1) net_USED = i;
				} else {
					print("--> Test Failed");
				}
			}
		}
	} catch (e) { }
	return true;
}

function GetDeviceIN() {
	my_net_in = wmi.Get( 'Win32_PerfRawData_Tcpip_NetworkInterface.name="' + net_ID[net_USED] + '"' ).Properties_.Item( "BytesReceivedPerSec" ).Value;

	if(my_net_in_last==0) my_net_in_last = my_net_in;
	my_net_in_diff = my_net_in - my_net_in_last;
	my_net_in_last = my_net_in;

	return my_net_in_diff/mod_netmonitor_timer.interval;
}

function GetDeviceOUT() {
	my_net_out = wmi.Get( 'Win32_PerfRawData_Tcpip_NetworkInterface.name="' + net_ID[net_USED] + '"' ).Properties_.Item( "BytesSentPerSec" ).Value;

	if(my_net_out_last==0) my_net_out_last = my_net_out;
	my_net_out_diff = my_net_out - my_net_out_last;
	my_net_out_last = my_net_out;

	return my_net_out_diff/mod_netmonitor_timer.interval;
}

function GetDeviceBW() {
	my_net_bw = wmi.Get( 'Win32_PerfRawData_Tcpip_NetworkInterface.name="' + net_ID[net_USED] + '"' ).Properties_.Item( "CurrentBandwidth" ).Value;
	return my_net_bw;
}

function TestDevice(DeviceName) {
	var my_test = wmi.Get( 'Win32_PerfRawData_Tcpip_NetworkInterface.name="' + DeviceName + '"' ).Properties_.Item( "CurrentBandwidth" ).Value;
	if(isNaN(my_test))
		return false;
	else
		return true;
}
